package com.changgou.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.file.pojo.FastDFSFile;
import com.changgou.file.util.FastDFSUtil;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/upload")
@CrossOrigin
public class FileUploadController {


    @PostMapping
    public Result upload(@RequestParam(value = "file")MultipartFile file) throws Exception {

        String[] upload = FastDFSUtil.upload(new FastDFSFile(
                file.getOriginalFilename(),
                file.getBytes(),
                StringUtils.getFilenameExtension(file.getOriginalFilename())//获取扩展名
        ));


        //拼接地址
        String filepath="http://192.168.192.192:8080/"+upload[0]+"/"+upload[1];
        return  new Result(true, StatusCode.OK,"上传成功",filepath);
    }

}
